﻿using System;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace BeeCoders.Tools.ControlCanvas.Adorners
{
    public class MoveThumb : Thumb
    {
        public DesignerItem DesignerItem;
        private DesignerCanvas designerCanvas;

        public MoveThumb()
        {
            DragStarted += MoveThumbDragStarted;
            DragDelta += MoveThumbDragDelta;
        }

        private void MoveThumbDragStarted(object sender, DragStartedEventArgs e)
        {
            DesignerItem = DataContext as DesignerItem;

            if (DesignerItem != null)
            {
                designerCanvas = VisualTreeHelper.GetParent(DesignerItem) as DesignerCanvas;
            }
        }

        private void MoveThumbDragDelta(object sender, DragDeltaEventArgs e)
        {
            if (DesignerItem != null && designerCanvas != null && DesignerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop = double.MaxValue;

                double maxLeft = 0;
                double maxTop = 0;

                double snapHorInterval = designerCanvas.SnapOffsetX;
                double snapVerInterval = designerCanvas.SnapOffsetY;


                foreach (DesignerItem item in designerCanvas.SelectedItems)
                {
                    minLeft = Math.Min(Canvas.GetLeft(item), minLeft);
                    minTop = Math.Min(Canvas.GetTop(item), minTop);

                    double width = double.IsNaN(item.Width) ? 20 : item.Width;
                    double height = double.IsNaN(item.Height) ? 20 : item.Height;

                    maxLeft = Math.Max(Canvas.GetLeft(item) + width, maxLeft);
                    maxTop = Math.Max(Canvas.GetTop(item) + height, maxTop);

                }

                Console.WriteLine(@"{0} {1} {2} {3} {4} {5}", minLeft, minTop, designerCanvas.Width, designerCanvas.Height, maxLeft, maxTop);

                double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                double deltaVertical = Math.Max(-minTop, e.VerticalChange);

                if (maxLeft + deltaHorizontal > designerCanvas.Width) deltaHorizontal = 0;
                if (maxLeft + deltaVertical > designerCanvas.Height) deltaVertical = 0;

                foreach (DesignerItem item in designerCanvas.SelectedItems)
                {
                    var newLeft = Canvas.GetLeft(item) + deltaHorizontal;
                    var newTop = Canvas.GetTop(item) + deltaVertical;

                    if (designerCanvas.IsSnapToGrid)
                    {
                        newLeft = snapHorInterval * Math.Round(newLeft / snapHorInterval);
                        newTop = snapVerInterval * Math.Round(newTop / snapVerInterval);

                    }

                    Canvas.SetLeft(item, newLeft);
                    Canvas.SetTop(item, newTop);

                    if (item.Control != null)
                    {
                        item.Control.IsDirty = true;

                        item.Control.LeftPosition = (int)newLeft;
                        item.Control.TopPosition = (int)newTop;
                    }
                }

                designerCanvas.InvalidateMeasure();
                e.Handled = true;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
